package com.fr.report.script;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.core.FRCoreContext;
import com.fr.base.file.FunctionManager;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.CellElementAttribute;
import com.fr.report.core.ScriptUtils;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.ColumnRowLiteral;
import com.fr.report.script.core.parser.ColumnRowRange;
import com.fr.report.script.core.parser.InterpreterError;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class AbstractNameSpace
  implements NameSpace
{
  private HashMap initedFunctionClasses;

  public Object getVariable(Object paramObject, Calculator paramCalculator)
  {
    if (paramObject instanceof ColumnRow)
      paramObject = ColumnRowRange.columnRow2Range((ColumnRow)paramObject);
    if (paramObject instanceof ColumnRowRange)
      return resolveColumnRowRange(paramCalculator, (ColumnRowRange)paramObject);
    String str = null;
    if (paramObject instanceof String)
      str = (String)paramObject;
    else if (paramObject instanceof ColumnRow)
      str = ((ColumnRow)paramObject).toString();
    if (str == null)
      return null;
    Object localObject = null;
    if (str.equalsIgnoreCase("null"))
      localObject = Primitive.NULL;
    else if (str.equalsIgnoreCase("nofilter"))
      localObject = Primitive.NOFILTER;
    else if (str.equalsIgnoreCase("true"))
      localObject = Boolean.TRUE;
    else if (str.equalsIgnoreCase("false"))
      localObject = Boolean.FALSE;
    else if ((str.equals("$$$")) || (str.matches("\\$\\w+")))
      localObject = Primitive.NULL;
    return localObject;
  }

  private Object resolveColumnRowRange(Calculator paramCalculator, ColumnRowRange paramColumnRowRange)
  {
    ColumnRowLiteral localColumnRowLiteral1 = paramColumnRowRange.getFrom();
    ColumnRowLiteral localColumnRowLiteral2 = paramColumnRowRange.getTo();
    ColumnRow localColumnRow1 = (localColumnRowLiteral1 == null) ? null : localColumnRowLiteral1.getTargetColumnRow();
    ColumnRow localColumnRow2 = (localColumnRowLiteral2 == null) ? null : localColumnRowLiteral2.getTargetColumnRow();
    if (localColumnRow1 == null)
      return Primitive.NULL;
    if (localColumnRow2 == null)
      localColumnRow2 = localColumnRow1;
    int i = Math.min(localColumnRow1.row, localColumnRow2.row);
    int j = Math.max(localColumnRow1.row, localColumnRow2.row);
    int k = Math.min(localColumnRow1.column, localColumnRow2.column);
    int l = Math.max(localColumnRow1.column, localColumnRow2.column);
    return resolveColumnRowIntersect(paramCalculator, k, i, l - k + 1, j - i + 1);
  }

  protected Object resolveColumnRowIntersect(Calculator paramCalculator, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    Report localReport = paramCalculator.getCurrentReport();
    if (localReport == null)
      return Primitive.NULL;
    ArrayList localArrayList = new ArrayList();
    Iterator localIterator = localReport.intersect(paramInt1, paramInt2, paramInt3, paramInt4);
    while (true)
    {
      Object localObject2;
      while (true)
      {
        while (true)
        {
          if (!(localIterator.hasNext()))
            break label162;
          localObject1 = (CellElement)localIterator.next();
          if (!(Boolean.TRUE.equals(((CellElement)localObject1).getAttribute(CellElementAttribute.DEL))))
            break;
        }
        localObject2 = ((CellElement)localObject1).getValue();
        if (!(localObject2 instanceof Formula))
          break;
        if (((Formula)localObject2).getResult() == null)
          ScriptUtils.executeCellElementFormula(paramCalculator, (Formula)localObject2, paramCalculator.getCurrentReport(), paramCalculator.getCurrentTableDataSource(), (CellElement)localObject1);
        localArrayList.add(((Formula)localObject2).getResult());
      }
      localArrayList.add(localObject2);
    }
    label162: Object localObject1 = null;
    if (localArrayList.size() == 0)
      localObject1 = Primitive.NULL;
    else if (localArrayList.size() == 1)
      localObject1 = localArrayList.get(0);
    else
      localObject1 = new FArray(localArrayList);
    return ((localObject1 == null) ? Primitive.NULL : localObject1);
  }

  public Function getMethod(Object paramObject, Calculator paramCalculator)
  {
    Object localObject;
    if (!(paramObject instanceof String))
      throw new InterpreterError(Inter.getLocText("Function_Name_Should_Not_Be") + paramObject + "");
    String str = ((String)paramObject).toUpperCase();
    if (this.initedFunctionClasses == null)
      this.initedFunctionClasses = new HashMap();
    Class localClass = (Class)this.initedFunctionClasses.get(str);
    if (localClass == null)
    {
      int j;
      localObject = null;
      FunctionManager localFunctionManager = FRContext.getFunctionManager();
      if (localFunctionManager != null)
      {
        int i = localFunctionManager.getFunctionDefCount();
        for (j = 0; j < i; ++j)
        {
          FunctionDef localFunctionDef = localFunctionManager.getFunctionDef(j);
          if (localFunctionDef.getName().equalsIgnoreCase(str))
            localObject = localFunctionDef.getClassName();
        }
      }
      if (localObject == null)
        localObject = "com.fr.report.script.function." + str;
      try
      {
        localClass = FRCoreContext.classForName((String)localObject);
      }
      catch (ClassNotFoundException localClassNotFoundException1)
      {
        if (!(((String)localObject).equals("com.fr.report.script.function." + str)))
          try
          {
            localClass = FRCoreContext.classForName("com.fr.report.script.function." + str);
          }
          catch (ClassNotFoundException localClassNotFoundException2)
          {
          }
      }
    }
    if (localClass == null)
      return null;
    this.initedFunctionClasses.put(str, localClass);
    try
    {
      localObject = localClass.newInstance();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, "new instance error", localException);
      return null;
    }
    return ((Function)(Function)localObject);
  }
}